#include "log_stream.h"

#ifdef UNDER_ANDROID

#include <android/log.h>

#define LOG_BUF_SIZE	1024

#endif

/*--------------------------------------------------------------------------*/
static FILE* logstream = NULL;

/*--------------------------------------------------------------------------*/
void logstream_to(FILE* f)
{
	logstream = f;
}


/*--------------------------------------------------------------------------*/
void logstream_write(const char *fmt, ...)
{
#ifdef UNDER_ANDROID
    va_list ap;
    char buf[LOG_BUF_SIZE];    

    va_start(ap, fmt);
    vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
    va_end(ap);

    __android_log_write(ANDROID_LOG_DEBUG, "SWAMPEX", buf);

#else

    if (logstream)
	{
        va_list ap;

        va_start(ap, fmt);

		vfprintf(logstream, fmt, ap);

        va_end(ap);
		
		fflush(logstream);
	}

#endif
}

/*--------------------------------------------------------------------------*/
void logstream_end()
{
	if (logstream)
    {
        fclose(logstream);
    }
}
